
package cn.base.web.gateway.context;

import java.util.function.Function;
import reactor.core.publisher.Mono;
import reactor.util.context.Context;



public final class ReactiveSecurityContextHolder {

	private static final Class<?> SECURITY_CONTEXT_KEY = SecurityContext.class;

	private ReactiveSecurityContextHolder() {
	}


	public static Mono<SecurityContext> getContext() {
		//从context取值
		return Mono.deferContextual(Mono::just)
				.cast(Context.class)
				.filter(ReactiveSecurityContextHolder::hasSecurityContext)
				.flatMap(ReactiveSecurityContextHolder::getSecurityContext);

	}

	private static boolean hasSecurityContext(Context context) {
		return context.hasKey(SECURITY_CONTEXT_KEY);
	}

	private static Mono<SecurityContext> getSecurityContext(Context context) {
		return context.<Mono<SecurityContext>>get(SECURITY_CONTEXT_KEY);
	}

	public static Function<Context, Context> clearContext() {
		return (context) -> context.delete(SECURITY_CONTEXT_KEY);
	}

	public static Context withSecurityContext(Mono<? extends SecurityContext> securityContext) {
		return Context.of(SECURITY_CONTEXT_KEY, securityContext);
	}


}
